package com.max.mysql.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBC_CRUD {
    public static void main(String[] args) {
//        query();
//        delete();
//        update();
//        save();
    }
    /**
     * 查询操作
     */
    public static void query(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            // 获得连接:
            conn = JDBCUtils.getConnection();
            // 编写SQL:
            String sql = "select * from droi_emp";
            // 预编译SQL:
            pstmt = conn.prepareStatement(sql);
            // 设置参数:
            // 执行SQL:
            rs = pstmt.executeQuery();
            // 遍历结果集:
            while(rs.next()){
                System.out.println(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(rs, pstmt, conn);
        }
    }

    /**
     * 删除操作
     */
    public static void delete(){
        Connection conn = null;
        PreparedStatement pstmt  = null;
        try{
            // 获得连接：
            conn = JDBCUtils.getConnection();
            // 编写SQL语句:
            String sql = "delete from droi_emp where emp_no = ?";
            // 预编译SQL
            pstmt = conn.prepareStatement(sql);
            // 设置参数:
            pstmt.setInt(1, 8);
            // 执行SQL:
            int num = pstmt.executeUpdate();
            if(num > 0){
                System.out.println("删除成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(pstmt, conn);
        }
    }

    /**
     * 修改操作
     */
    public static void update(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try{
            // 获得连接:
            conn = JDBCUtils.getConnection();
            // 编写SQL语句:
            String sql = "update droi_emp set ename = ?,password =? where emp_no = ?";
            // 预编译SQL:
            pstmt = conn.prepareStatement(sql);
            // 设置参数:
            pstmt.setString(1, "李时珍");
            pstmt.setString(2, "9999");
            pstmt.setString(3, "8");
            // 执行SQL：
            int num = pstmt.executeUpdate();
            if(num > 0){
                System.out.println("修改成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(pstmt, conn);
        }
    }

    /**
     * 保存操作
     */
    public static void save(){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try{
            // 获得连接:
            conn = JDBCUtils.getConnection();
            // 编写SQL语句:
            String sql = "insert into droi_emp(ename, password) values (?, ?)";
            // 预编译SQL:
            pstmt = conn.prepareStatement(sql);
            // 设置参数:
            pstmt.setString(1, "华佗");
            pstmt.setString(2, "888888");
            // 执行SQL
            int num = pstmt.executeUpdate();
            if(num > 0){
                System.out.println("保存成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            JDBCUtils.release(pstmt, conn);
        }
    }
}
